// LuoDndTest.cpp : main source file for LuoDndTest.exe
//

#include "stdafx.h"

#include <atlframe.h>
#include <atlctrls.h>
#include <atldlgs.h>

#include "resource.h"

#include "aboutdlg.h"
#include "MainDlg.h"

// Include header files for drag and drop.
#include "include/LuoDnDAssist.h"
#include "include/LuoDndAssistImport.h"

// Import lib
#ifdef _DEBUG
#pragma comment(lib, "bin/LuoDnd_Debug.lib")
#else
#pragma comment(lib, "bin/LuoDnd.lib")
#endif

/* */
extern LuoDnDAssist::UActivator* g_pDnDAssist;

const UINT CMainDlg::CF_FILECONTENTS         = RegisterClipboardFormatW(L"FileContents");
const UINT CMainDlg::CF_FILEGROUPDESCRIPTORW = RegisterClipboardFormatW(L"FileGroupDescriptorW");
const UINT CMainDlg::CF_HTMLFORMAT           = RegisterClipboardFormatW(L"HTML Format");

CAppModule _Module;

LuoDnDAssist::UActivator* g_pDnDAssist;

int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT)
{
	CMessageLoop theLoop;
	_Module.AddMessageLoop(&theLoop);

	CMainDlg dlgMain;

	if(dlgMain.Create(NULL) == NULL)
	{
		ATLTRACE(_T("Main dialog creation failed!\n"));
		return 0;
	}

	dlgMain.ShowWindow(nCmdShow);

	int nRet = theLoop.Run();

	_Module.RemoveMessageLoop();
	return nRet;
}


/**
* @brief      : Initialize LuoDnd
* @param      : 
* @return     : bool
*/
bool InitLuoDnd()
{
	HRESULT hRslt = LuoDnDAssist::Activate_import(
		L"activator",
		(void**)&g_pDnDAssist);

	if (FAILED(hRslt)) {
		return false;
	}

	hRslt = g_pDnDAssist->Initialize();
	if (FAILED(hRslt)) {
		g_pDnDAssist->Uninitialize();
		g_pDnDAssist = NULL;
		return false;
	}

	return true;
}

/**
* @brief      : Uninitialize LuoDnd
* @param      : 
* @return     : 
*/
void UninitLuoDnd()
{
	if (g_pDnDAssist != NULL) {
		g_pDnDAssist->Uninitialize();
		g_pDnDAssist = NULL;
	}
}

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
	HRESULT hRes = ::CoInitialize(NULL);
// If you are running on NT 4.0 or higher you can use the following call instead to 
// make the EXE free threaded. This means that calls come in on a random RPC thread.
//	HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
	ATLASSERT(SUCCEEDED(hRes));

	// for drag and drop.
	HRESULT hRslt = OleInitialize(0);
	if (FAILED(hRslt)) {
		return -1;
	}

	// this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
	::DefWindowProc(NULL, 0, 0, 0L);

	AtlInitCommonControls(ICC_BAR_CLASSES);	// add flags to support other controls

	// Initialize GDI++
	ULONG_PTR gdiplusToken;
	Gdiplus::GdiplusStartupInput gsi;
	Gdiplus::GdiplusStartup(&gdiplusToken, &gsi, 0);

	hRes = _Module.Init(NULL, hInstance);
	ATLASSERT(SUCCEEDED(hRes));

	InitLuoDnd();

	int nRet = Run(lpstrCmdLine, nCmdShow);

	UninitLuoDnd();

	_Module.Term();

	// Unintialize GDI++
	Gdiplus::GdiplusShutdown(gdiplusToken);

	// for drag and drop.
	OleUninitialize();

	::CoUninitialize();

	return nRet;
}
